<template>
  <div class="app-container user">
    <SearchForm v-model:form-data="form" @search="handleSearch" />
    <PaginationCard
      v-model:current-page="paginationData.currentPage"
      v-model:page-size="paginationData.pageSize"
      :total="paginationData.total"
      :load-list="getTableData"
      v-loading="isLoading"
    >
      <AppTable :data="tableData" :columns="columns" border />
    </PaginationCard>
    <EntityChangeDetail
      v-model="visible"
      v-model:entity-change-id="entityChangeId"
      size="40%"
    />
  </div>
</template>

<script lang="tsx" setup>
import { computed, onMounted, ref } from 'vue'

import AppButtonGroup from '@/components/AppButtonGroup/index.vue'
import { IButtonConfig } from '@/components/AppButtonGroup/type'
import AppTable from '@/components/AppTable/index.vue'
import PaginationCard from '@/components/PaginationCard/index.vue'
import usePagination from '@/hooks/usePagination'
import { getEntityChangeLog } from '@/http/entityChangeLog'
import { EntityLogEnum, IEntityChangeLog } from '@/http/types/entityChangeLog'
import { parseDate2Str } from '@/utils/timeUtils'
import EntityChangeDetail from '@/views/system/auditLog/EntityChangeDetail.vue'

import SearchForm from './SearchForm.vue'

const buttonConfig: IButtonConfig[] = [
  {
    name: '详细信息',
    eventName: 'handleDetail',
    type: 'primary',
  },
]
const columns = ref([
  { label: 'AuditLogId', prop: 'auditLogId' },
  { label: 'EntityTypeFullName', prop: 'entityTypeFullName' },
  { label: 'EntityId', prop: 'entityId' },
  {
    label: 'ChangeType',
    prop: 'changeType',
    render: ({ row }: { row: IEntityChangeLog }) =>
      EntityLogEnum[row.changeType],
  },
  {
    label: 'ChangeTime',
    prop: 'ChangeTime',
    render: ({ row }: { row: IEntityChangeLog }) =>
      parseDate2Str(row.changeTime, 'YYYY-MM-DD HH:mm:ss'),
  },
  {
    label: '操作',
    fixed: 'right',
    width: 220,
    render: ({ row }: { row: IEntityChangeLog }) => {
      return (
        <AppButtonGroup
          rowData={row}
          buttonConfigs={buttonConfig}
          onEvent={(eventName: string) => {
            eventName === 'handleDetail' && handleDetail(row)
          }}
        ></AppButtonGroup>
      )
    },
  },
])

interface IFormData {
  auditLogId: string
  date: [string | Date, string | Date]
  changeType: 0 | 1 | 2 | ''
  entityId: string
  entityTypeFullName: string
}
const form = ref<IFormData>({
  auditLogId: '',
  date: ['', ''],
  changeType: '',
  entityId: '',
  entityTypeFullName: '',
})

onMounted(() => {
  getTableData()
})

const params = computed(() => {
  return {
    auditLogId: form.value.auditLogId,
    startTime: form.value.date[0],
    endTime: form.value.date[1],
    changeType: form.value.changeType,
    entityId: form.value.entityId,
    entityTypeFullName: form.value.entityTypeFullName,
  }
})
const {
  isLoading,
  paginationData,
  tableData,
  getTableData,
  handleSearch,
  // handleSortChange
} = usePagination(getEntityChangeLog, params)

const visible = ref(false)
const entityChangeId = ref('')
const handleDetail = (detail: IEntityChangeLog) => {
  entityChangeId.value = detail.id
  visible.value = true
}
</script>

<style lang="scss" scoped></style>
